//
//  PitSelectionViewController.m
//  bbqpitbooking
//
//  Created by Le Moe Thwin on 5/21/11.
//  Copyright 2011 iss. All rights reserved.
//

#import "PitSelectionViewController.h"
#import "ParticularsEntryViewController.h"
#import "BBQPitBooking.h";
#import "BBQPit.h";
#import "Park.h";
#import "ParkArea.h";
#import "JSONRPCService.h"

@implementation PitSelectionViewController

@synthesize pitArray,booking,bookingDate,webSVC,selectedArea,p,a;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.title = @"Select BBQ Pit";
	
	webSVC = [[JSONRPCService alloc] init];
	
	NSString *parkName = self.selectedArea.park.parkName;
	NSString *parkAreaName = self.selectedArea.areaName;
	
	NSString *key1 = nil;
	self.pitArray = [[NSMutableDictionary alloc] init];
	
	NSMutableArray *pitList = [webSVC getPitsByParkAndArea:parkName andParkArea:parkAreaName];	
	
	for(BBQPit *pit in pitList){
		//pitInArray = [[BBQPit alloc] init];
		pit.parkArea = self.selectedArea;
		
		key1 = nil;
		key1 = [NSString stringWithFormat:@"%@/%@/%@", pit.parkArea.park.parkName, 
						  pit.parkArea.areaName,pit.assignedPitName];
		
		[self.pitArray setObject:pit forKey:key1];
	}
		
	[webSVC release];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
	[pitArray release];
	[booking release];
}

- (void)dealloc {
    [super dealloc];
}

- (IBAction) Continue
{
	BBQPit *pit;
	self.booking = [[BBQPitBooking alloc] init];
	
	NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
	
	//Get all pits with status "Y" and overwrite BBQPit array in booking object
	for (NSString *keyInput in [self.pitArray allKeys]){
		pit = [self.pitArray valueForKey:keyInput];
		if ([pit.status isEqualToString:@"Y"]) {
			[dict setObject:pit forKey:keyInput];
		}
	}
	
	//selected pits
	booking.selectedPits = dict;

	NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
	[dateFormat setDateFormat:@"MMMM dd, YYYY"];
	NSDate *date = [dateFormat dateFromString:self.bookingDate];

	self.booking.bookingDate = date;
	[dateFormat release];
	

	if ([dict count] == 0) {
		UIAlertView *alertPopUp=[[UIAlertView alloc]
								 initWithTitle:@"BBQ Pit Booking" message:@"Please select pits to continue" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alertPopUp show];
		[alertPopUp release];
		return;
	}
	
	ParticularsEntryViewController *particularsEntryViewController = [[ParticularsEntryViewController alloc]
															  initWithNibName:@"ParticularsEntryViewController"
															  bundle:nil];
	particularsEntryViewController.booking = self.booking;
	particularsEntryViewController.booking.bookingID = self.booking.bookingID;
	
	[self.navigationController 
	 pushViewController:particularsEntryViewController
	 animated:YES];
	[particularsEntryViewController release];
}

//TableView Methods
//Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.pitArray count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell
	NSString *pitKey = [[self.pitArray allKeys] objectAtIndex: indexPath.row];
    BBQPit *pit = [self.pitArray objectForKey:pitKey];
	
	NSString *parkAndArea = [NSString stringWithFormat:@"%@/%@",pit.parkArea.park.parkName,pit.parkArea.areaName];
	
	cell.textLabel.text = parkAndArea;
	cell.detailTextLabel.text = pit.assignedPitName;
	
    cell.accessoryType = UITableViewCellAccessoryNone;
	//cell.font =[UIFont fontWithName:@"Helvetica" size:12.0];
	[cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
    return cell;
}


- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
	NSString *selectKey = [NSString stringWithFormat:@"%@/%@",cell.textLabel.text,cell.detailTextLabel.text];
	
	//NSLog(@"@selected key:%@",selectKey);
	BBQPit *pit;
	pit =  [self.pitArray objectForKey:selectKey];
	
	if ([cell accessoryType] == UITableViewCellAccessoryCheckmark){
		//NSLog(@"@BeforeSelection:%@",pit.status);
		[cell setAccessoryType:UITableViewCellAccessoryNone];
		pit.status = @"N";
		
	}else {
		//NSLog(@"@BeforeSelection:%@",pit.status);
		[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
		pit.status = @"Y";
	}
}


@end
